package com.coderly.onlinegallerysharingplatform.common.web;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.coderly.onlinegallerysharingplatform.common.convention.errorcode.ErrorCode;
import com.coderly.onlinegallerysharingplatform.common.response.Response;
import com.coderly.onlinegallerysharingplatform.common.convention.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BizException.class)
    public Response<?> businessExceptionHandler(BizException e) {
        log.error("BusinessExceptionHandler: ", e);
        return Response.error(e.getErrorCode(), e.getMessage());
    }

    @ExceptionHandler({ MethodArgumentNotValidException.class})
    @ResponseBody
    public Response<?> handleMethodArgumentNotValidException(HttpServletRequest request, MethodArgumentNotValidException e) {
        // 获取参数异常码
        int errorCode = ErrorCode.PARAMS_ERROR.getCode();

        // 获取 BindingResult
        BindingResult bindingResult = e.getBindingResult();
        // 错误信息
        String errorMessage = null;

        // 获取校验不通过的字段，并组合错误信息，格式为： email 邮箱格式不正确, 当前值: '123124qq.com';
        // 获取第一个校验不通过的字段，并组合错误信息
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        if (!fieldErrors.isEmpty()) {
            org.springframework.validation.FieldError firstError = fieldErrors.get(0);
            errorMessage = firstError.getDefaultMessage();
        }

        log.warn("{} request error, errorCode: {}, errorMessage: {}", request.getRequestURI(), errorCode, errorMessage);
        return Response.error(errorCode, errorMessage);

    }

    @ExceptionHandler(RuntimeException.class)
    public Response<?> businessExceptionHandler(RuntimeException e) {
        log.error("RuntimeException: ", e);
        return Response.error(ErrorCode.SYSTEM_ERROR, ErrorCode.SYSTEM_ERROR.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public Response<?> handleNotLoginException(NotLoginException e) {
        log.error("NotLoginException", e);
        return Response.error(ErrorCode.NOT_LOGIN_ERROR, ErrorCode.NOT_LOGIN_ERROR.getMessage());
    }

    @ExceptionHandler(NotRoleException.class)
    public Response<?> handleNotRoleException(NotRoleException e) {
        log.error("NotLoginException", e);
        return Response.error(ErrorCode.NO_AUTH_ERROR, ErrorCode.NO_AUTH_ERROR.getMessage());
    }

    @ExceptionHandler(NotPermissionException.class)
    public Response<?> handleNotPermissionException(NotPermissionException e) {
        log.error("NotPermissionException", e);
        return Response.error(ErrorCode.NO_AUTH_ERROR, ErrorCode.NO_AUTH_ERROR.getMessage());
    }



}
